package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.bean.message.template.TemplateDate;
import com.xmy.cultivate.bean.message.template.TemplateMessage;
import com.xmy.cultivate.entity.SystemErrorLog;
import com.xmy.cultivate.entity.WecharTemplate;
import com.xmy.cultivate.entity.WecharsBind;
import com.xmy.cultivate.feign.WecharFeignClient;
import com.xmy.cultivate.mapper.WecharTemplateMapper;
import com.xmy.cultivate.mapper.WecharsBindMapper;
import com.xmy.cultivate.service.IMqPushService;
import com.xmy.cultivate.service.ISystemErrorLogService;
import com.xmy.cultivate.service.IWecharsBindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 微信绑定 服务实现类
 * </p>
 *
 * @author min
 * @since 2023-01-12
 */
@Service
public class WecharsBindServiceImpl extends ServiceImpl<WecharsBindMapper, WecharsBind> implements IWecharsBindService {


    @Autowired
    WecharsBindMapper wecharsBindMapper;

    @Autowired
    WecharFeignClient wecharFeignClient;

    @Autowired
    WecharTemplateMapper wecharTemplateMapper;

    @Autowired
    ISystemErrorLogService iSystemErrorLogService;

    @Autowired
    IMqPushService iMqPushService;

    @Autowired
    @Lazy
    WecharsBindServiceImpl wecharsBindService;


    /**
     * 修改绑定的学生
     */
    public void updateBindStudent(){

    }

    public List<String> getSendOpenid(Long studentId){
        return wecharsBindMapper.getSendOpenid(studentId);
    }

    public void useCourseHouse(List<String> openidList, String firstParam, String keyword1Param, String keyword2Param, String keyword3Param, String keyword4Param, String remarkParam, String clientMsgId){

        try{
            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",3);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();
                //System.out.println("template_id:"+template_id);
                for (String opendId:openidList){
                    String touser= opendId;
                    String url="";
                    HashMap<String,String> first = new HashMap<>();
                    first.put("value",firstParam);
                    first.put("color","#33A1C9");
                    HashMap<String,String> keyword1 = new HashMap<>();
                    keyword1.put("value",keyword1Param);
                    keyword1.put("color","#33A1C9");
                    HashMap<String,String> keyword2 = new HashMap<>();
                    keyword2.put("value",keyword2Param);
                    keyword2.put("color","#33A1C9");
                    HashMap<String,String> remark = new HashMap<>();
                    remark.put("value",remarkParam);
                    remark.put("color","#FF8000");
                    TemplateDate templateDate = TemplateDate.builder().first(first).keyword1(keyword1).keyword2(keyword2).remark(remark).build();
                    TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).client_msg_id(clientMsgId).data(templateDate).build();

                    String obj = templateMessage.toJson();
                    iMqPushService.wecharPush(obj);
                }
            }else {
                //System.out.println("模板没有找到wecharTemplate");
            }
        }catch (Exception e){
            //System.out.println("temp发送失败");
        }
    }


    /**
     * 到离校提醒
     * @param openidList
     * @param firstParam
     * @param keyword1Param
     * @param keyword2Param
     * @param remarkParam
     * @param clientMsgId
     */
    @Override
    public void onOffSchool(List<String> openidList, String firstParam, String keyword1Param, String keyword2Param, String keyword3Param, String remarkParam, String url, String clientMsgId){

        try{
            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",2);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();
                //System.out.println("template_id:"+template_id);
                for (String opendId:openidList){
                    String touser= opendId;
                    HashMap<String,String> first = new HashMap<>();
                    first.put("value",firstParam);
                    first.put("color","#33A1C9");
                    HashMap<String,String> keyword1 = new HashMap<>();
                    keyword1.put("value",keyword1Param);
                    keyword1.put("color","#33A1C9");
                    HashMap<String,String> keyword2 = new HashMap<>();
                    keyword2.put("value",keyword2Param);
                    keyword2.put("color","#33A1C9");

                    HashMap<String,String> keyword3 = new HashMap<>();
                    keyword3.put("value",keyword3Param);
                    keyword3.put("color","#33A1C9");

                    HashMap<String,String> remark = new HashMap<>();
                    remark.put("value",remarkParam);
                    remark.put("color","#FF8000");
                    TemplateDate templateDate = TemplateDate.builder().first(first).keyword1(keyword1).keyword2(keyword2).keyword3(keyword3).remark(remark).build();
                    TemplateMessage templateMessage;
                    if (StringUtils.isEmpty( url)){
                        templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).client_msg_id(clientMsgId).data(templateDate).build();
                    }else {
                        templateMessage = TemplateMessage.builder().url(url).touser(touser).template_id(template_id).client_msg_id(clientMsgId).data(templateDate).build();
                    }
                    String obj = templateMessage.toJson();
                    iMqPushService.wecharPush(obj);
                    //String ss =  wecharFeignClient.sendWecharMsg(obj);
                }
            }else {
                //System.out.println("模板没有找到wecharTemplate");
                SystemErrorLog systemErrorLog = new SystemErrorLog();
                systemErrorLog.setContent("模板没有找到wecharTemplate");
                systemErrorLog.setType(3);
                systemErrorLog.setRemark("签到或签退微信推送");
                iSystemErrorLogService.save(systemErrorLog);
            }
        }catch (Exception e){
            //System.out.println("temp发送失败");
            SystemErrorLog systemErrorLog = new SystemErrorLog();
            systemErrorLog.setContent(e.getMessage());
            systemErrorLog.setType(3);
            systemErrorLog.setRemark("签到或签退微信推送");
            iSystemErrorLogService.save(systemErrorLog);

        }
    }


    /**
     * 到离校提醒Mq推送
     * @param opendId
     * @param firstParam
     * @param keyword1Param
     * @param keyword2Param
     * @param remarkParam
     * @param clientMsgId
     */
    @Override
    public void onOffSchoolMq(String opendId, String firstParam, String keyword1Param, String keyword2Param, String keyword3Param, String remarkParam, String clientMsgId){

        try{
            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",2);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();
                //System.out.println("template_id:"+template_id);
                String touser= opendId;
                String url="";
                HashMap<String,String> first = new HashMap<>();
                first.put("value",firstParam);
                first.put("color","#33A1C9");
                HashMap<String,String> keyword1 = new HashMap<>();
                keyword1.put("value",keyword1Param);
                keyword1.put("color","#33A1C9");
                HashMap<String,String> keyword2 = new HashMap<>();
                keyword2.put("value",keyword2Param);
                keyword2.put("color","#33A1C9");

                HashMap<String,String> keyword3 = new HashMap<>();
                keyword3.put("value",keyword3Param);
                keyword3.put("color","#33A1C9");

                HashMap<String,String> remark = new HashMap<>();
                remark.put("value",remarkParam);
                remark.put("color","#FF8000");
                TemplateDate templateDate = TemplateDate.builder().first(first).keyword1(keyword1).keyword2(keyword2).keyword3(keyword3).remark(remark).build();
                TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).client_msg_id(clientMsgId).data(templateDate).build();

                String obj = templateMessage.toJson();
                iMqPushService.wecharPush(obj);
            }else {
                //System.out.println("模板没有找到wecharTemplate");
                SystemErrorLog systemErrorLog = new SystemErrorLog();
                systemErrorLog.setContent("模板没有找到wecharTemplate");
                systemErrorLog.setType(3);
                systemErrorLog.setRemark("签到或签退微信推送");
                iSystemErrorLogService.save(systemErrorLog);
            }
        }catch (Exception e){
            //System.out.println("temp发送失败");
            SystemErrorLog systemErrorLog = new SystemErrorLog();
            systemErrorLog.setContent(e.getMessage());
            systemErrorLog.setType(3);
            systemErrorLog.setRemark("签到或签退微信推送");
            iSystemErrorLogService.save(systemErrorLog);

        }
    }



    /**
     * 上课提醒
     * @param openidList
     * @param firstParam
     * @param keyword1Param
     * @param keyword2Param
     * @param remarkParam
     * @param clientMsgId
     */
    public void onCourse(List<String> openidList, String firstParam, String keyword1Param, String keyword2Param, String remarkParam, String clientMsgId){

        try{
            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",1);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();
                //System.out.println("template_id:"+template_id);
                for (String opendId:openidList){
                    String touser= opendId;
                    String url="";
                    HashMap<String,String> first = new HashMap<>();
                    first.put("value",firstParam);
                    first.put("color","#33A1C9");
                    HashMap<String,String> keyword1 = new HashMap<>();
                    keyword1.put("value",keyword1Param);
                    keyword1.put("color","#33A1C9");
                    HashMap<String,String> keyword2 = new HashMap<>();
                    keyword2.put("value",keyword2Param);
                    keyword2.put("color","#33A1C9");
                    HashMap<String,String> remark = new HashMap<>();
                    remark.put("value",remarkParam);
                    remark.put("color","#FF8000");
                    TemplateDate templateDate = TemplateDate.builder().first(first).keyword1(keyword1).keyword2(keyword2).remark(remark).build();
                    TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).client_msg_id(clientMsgId).data(templateDate).build();

                    String obj = templateMessage.toJson();
                    iMqPushService.wecharPush(obj);
                }
            }else {
                //System.out.println("模板没有找到wecharTemplate");
            }
        }catch (Exception e){
            //System.out.println("temp发送失败");
        }
    }

    /**
     * 预分班提醒
     * @param openidList
     * @param firstParam
     * @param keyword1Param
     * @param keyword2Param
     * @param remarkParam
     * @param clientMsgId
     */
    public void presortGrade(List<String> openidList, String firstParam, String keyword1Param, String keyword2Param, String remarkParam, String clientMsgId){

        try{
            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",7);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();
                //System.out.println("template_id:"+template_id);
                for (String opendId:openidList){
                    String touser= opendId;
                    String url="";
                    HashMap<String,String> first = new HashMap<>();
                    first.put("value",firstParam);
                    first.put("color","#33A1C9");
                    HashMap<String,String> keyword1 = new HashMap<>();
                    keyword1.put("value",keyword1Param);
                    keyword1.put("color","#33A1C9");
                    HashMap<String,String> keyword2 = new HashMap<>();
                    keyword2.put("value",keyword2Param);
                    keyword2.put("color","#33A1C9");
                    HashMap<String,String> remark = new HashMap<>();
                    remark.put("value",remarkParam);
                    remark.put("color","#FF8000");
                    TemplateDate templateDate = TemplateDate.builder().first(first).keyword1(keyword1).keyword2(keyword2).remark(remark).build();
                    TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).client_msg_id(clientMsgId).data(templateDate).build();

                    String obj = templateMessage.toJson();
                    iMqPushService.wecharPush(obj);
                }
            }else {
                //System.out.println("模板没有找到wecharTemplate");
            }
        }catch (Exception e){
            //System.out.println("temp发送失败");
        }
    }


    /**
     * 成绩统计推送
     * @param openidList
     * @param firstParam
     * @param keyword1Param
     * @param keyword2Param
     * @param keyword3Param
     * @param remarkParam
     * @param url
     * @param clientMsgId
     * @return
     */
    @Override
    public Boolean wxPushPartriach(List<String> openidList, String firstParam, String keyword1Param, String keyword2Param, String keyword3Param, String remarkParam,String url, String clientMsgId){
        try{
            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",6);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();
                //System.out.println("template_id:"+template_id);
                for (String opendId:openidList){
                    String touser= opendId;
                    //String url="";
                    HashMap<String,String> first = new HashMap<>();
                    first.put("value",firstParam);
                    first.put("color","#33A1C9");
                    HashMap<String,String> keyword1 = new HashMap<>();
                    keyword1.put("value",keyword1Param);
                    keyword1.put("color","#33A1C9");
                    HashMap<String,String> keyword2 = new HashMap<>();
                    keyword2.put("value",keyword2Param);
                    keyword2.put("color","#33A1C9");
                    HashMap<String,String> keyword3 = new HashMap<>();
                    keyword3.put("value",keyword3Param);
                    keyword3.put("color","#33A1C9");
                    HashMap<String,String> remark = new HashMap<>();
                    remark.put("value",remarkParam);
                    remark.put("color","#FF8000");
                    TemplateDate templateDate = TemplateDate.builder().first(first).keyword1(keyword1).keyword2(keyword2).keyword3(keyword3).remark(remark).build();
                    TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).url(url).client_msg_id(clientMsgId).data(templateDate).build();

                    String obj = templateMessage.toJson();
                    iMqPushService.wecharPush(obj);
                }
                return true;
            }else {
                System.out.println("模板没有找到wecharTemplate");
                return false;
            }
        }catch (Exception e){
            System.out.println("temp发送失败"+e.getMessage());
            return false;
        }
    }

    /**
     * 上课提醒
     * @param openidList
     * @param firstParam
     * @param keyword1Param
     * @param keyword2Param
     * @param remarkParam
     * @param clientMsgId
     */
    @Override
    public void wxPushOnCourse(List<String> openidList, String firstParam, String keyword1Param, String keyword2Param, String remarkParam, String clientMsgId){

        try{
            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",1);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();
                //System.out.println("template_id:"+template_id);
                for (String opendId:openidList){
                    String touser= opendId;
                    String url="";
                    HashMap<String,String> first = new HashMap<>();
                    first.put("value",firstParam);
                    first.put("color","#33A1C9");
                    HashMap<String,String> keyword1 = new HashMap<>();
                    keyword1.put("value",keyword1Param);
                    keyword1.put("color","#33A1C9");
                    HashMap<String,String> keyword2 = new HashMap<>();
                    keyword2.put("value",keyword2Param);
                    keyword2.put("color","#33A1C9");
                    HashMap<String,String> remark = new HashMap<>();
                    remark.put("value",remarkParam);
                    remark.put("color","#FF8000");
                    TemplateDate templateDate = TemplateDate.builder().first(first).keyword1(keyword1).keyword2(keyword2).remark(remark).build();
                    TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).client_msg_id(clientMsgId).data(templateDate).build();

                    String obj = templateMessage.toJson();
                    iMqPushService.wecharPush(obj);
                }
            }else {
                //System.out.println("模板没有找到wecharTemplate");
            }
        }catch (Exception e){
            //System.out.println("temp发送失败");
        }
    }


    /**
     * 上课提醒
     * @param openidList
     * @param firstParam
     * @param keyword1Param
     * @param keyword2Param
     * @param remarkParam
     * @param clientMsgId
     */
    @Override
    public void wxCalOnCourse(List<String> openidList, String firstParam, String keyword1Param, String keyword2Param, String remarkParam, String clientMsgId){

        try{
            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",8);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();
                //System.out.println("template_id:"+template_id);
                for (String opendId:openidList){
                    String touser= opendId;
                    String url="";
                    HashMap<String,String> first = new HashMap<>();
                    first.put("value",firstParam);
                    first.put("color","#33A1C9");
                    HashMap<String,String> keyword1 = new HashMap<>();
                    keyword1.put("value",keyword1Param);
                    keyword1.put("color","#33A1C9");
                    HashMap<String,String> keyword2 = new HashMap<>();
                    keyword2.put("value",keyword2Param);
                    keyword2.put("color","#33A1C9");
                    HashMap<String,String> remark = new HashMap<>();
                    remark.put("value",remarkParam);
                    remark.put("color","#FF8000");
                    TemplateDate templateDate = TemplateDate.builder().first(first).keyword1(keyword1).keyword2(keyword2).remark(remark).build();
                    TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).template_id(template_id).client_msg_id(clientMsgId).data(templateDate).build();

                    String obj = templateMessage.toJson();
                    iMqPushService.wecharPush(obj);
                }
            }else {
                //System.out.println("模板没有找到wecharTemplate");
            }
        }catch (Exception e){
            //System.out.println("temp发送失败");
        }
    }


    /**
     * 创建订单推送给家长
     * @param firstParam
     * @param keyword1Param
     * @param keyword2Param

     */
    public void pushOrder( String firstParam, String keyword1Param, String keyword2Param,  String studentId,String lklOrderNo){

        try{

            String url= "http://parent.5ixmy.xyz/orderDetails?lklOrderNo="+lklOrderNo;

            //获取要发送的家长
            List<String> openidList = wecharTemplateMapper.openidList(studentId);

            QueryWrapper<WecharTemplate> wecharTemplateQueryWrapper = new QueryWrapper<>();
            wecharTemplateQueryWrapper.eq("status",1);
            wecharTemplateQueryWrapper.eq("type",11);
            wecharTemplateQueryWrapper.last("limit 1");
            WecharTemplate wecharTemplate = wecharTemplateMapper.selectOne(wecharTemplateQueryWrapper);
            if(wecharTemplate != null){
                String template_id = wecharTemplate.getTemplateId();

                if (openidList != null && !openidList.isEmpty()) {
                    for (String opendId : openidList) {
                        String touser = opendId;
                        HashMap<String, String> thing1 = new HashMap<>();
                        thing1.put("value", firstParam);
                        thing1.put("color", "#33A1C9");
                        HashMap<String, String> thing19 = new HashMap<>();
                        thing19.put("value", keyword1Param);
                        thing19.put("color", "#33A1C9");
                        HashMap<String, String> amount9 = new HashMap<>();
                        amount9.put("value", keyword2Param);
                        amount9.put("color", "#33A1C9");


                        TemplateDate templateDate = TemplateDate.builder().thing1(thing1).thing19(thing19).amount9(amount9).build();

                        TemplateMessage templateMessage = TemplateMessage.builder().touser(touser).url(url).template_id(template_id).client_msg_id(lklOrderNo).data(templateDate).build();

                        String obj = templateMessage.toJson();
                        //System.out.println("obj:"+obj);
                        iMqPushService.wecharPush(obj);
                        //String ss =  wecharFeignClient.sendWecharMsg(obj);
                    }
                }
            }else {
                //System.out.println("模板没有找到wecharTemplate");
                SystemErrorLog systemErrorLog = new SystemErrorLog();
                systemErrorLog.setContent("模板没有找到wecharTemplate");
                systemErrorLog.setType(3);
                systemErrorLog.setRemark("创建订单推送公众号");
                iSystemErrorLogService.save(systemErrorLog);
            }
        }catch (Exception e){
            //System.out.println("temp发送失败");
            SystemErrorLog systemErrorLog = new SystemErrorLog();
            systemErrorLog.setContent(e.getMessage());
            systemErrorLog.setType(3);
            systemErrorLog.setRemark("创建订单推送公众号");
            iSystemErrorLogService.save(systemErrorLog);

        }
    }




}
